package by.alex.dzeshko.lastfmsimple.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore.Audio.AlbumColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.util.Log;
import by.alex.dzeshko.lastfmsimple.R;
import by.alex.dzeshko.lastfmsimple.app.fragment.AlbumsDetailFragment;
import by.alex.dzeshko.lastfmsimple.app.fragment.AlbumsListFragment.Callbacks;
import by.alex.dzeshko.lastfmsimple.app.fragment.ArtistDetailFragment;
import by.alex.dzeshko.lastfmsimple.app.fragment.TrackDetailFragment;
import by.alex.dzeshko.lastfmsimple.appstate.AppState.ScreenState;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

public class DetailActivity extends SherlockFragmentActivity implements Callbacks {

	public static final String STATE = "STATE";

	public static final String COVER = "COVER";
	
	public static final String MBID = "MBID";
	
	public static final String MARKER = "MARKER";
	
	public static final String ARGUMENTS = "ARGUMENTS";

	private static final String LOG_TAG = "DetailActivity";

	public static final String ARTIST = "ARTIST";

	public static final String ALBUM = "ALBUM";
	
	private ScreenState mScreenState;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		mScreenState = (ScreenState) intent.getSerializableExtra(STATE);
		setContentView(R.layout.activity_details);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		if (savedInstanceState == null) {
			// Create the detail fragment and add it to the activity
			// using a fragment transaction.
			Log.d(LOG_TAG, "savedInstanceState null");
			Bundle arguments = intent.getBundleExtra(ARGUMENTS);
			Fragment fragment;
			switch (mScreenState) {
			case ALBUM:
				fragment = new AlbumsDetailFragment();
				fragment.setArguments(arguments);
				getSupportFragmentManager().beginTransaction()
						.add(R.id.detail_container, fragment).commit();
				break;
			case ARTIST:
				fragment = new ArtistDetailFragment();
				fragment.setArguments(arguments);
				getSupportFragmentManager().beginTransaction()
						.add(R.id.detail_container, fragment).commit();
				break;
			/*case TRACK:
				fragment = new TrackDetailFragment();
				fragment.setArguments(arguments);
				getSupportFragmentManager().beginTransaction()
						.add(R.id.detail_container, fragment).commit();
				break;*/
			default:
				break;
			}
			
			
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onItemSelected(ScreenState state, Bundle arguments) {
		Fragment fragment = new AlbumsDetailFragment();
		fragment.setArguments(arguments);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.detail_container, fragment).addToBackStack(null).commit();
		
	}

	@Override
	public void onSearchQuerySubmited(String query) {
		// TODO Auto-generated method stub
		
	}

}
